package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCDemo1 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        /*
        使用JDBC链接数据库并操作的基本流程
        1：加载驱动
        2：使用DriverManager利用驱动与数据库建立链接，得到Connection对象
        3：使用Connection对象，得到Statement对象
         */
        Class.forName("com.mysql.cj.jdbc.Driver");
        //2
        Connection connection=DriverManager.getConnection(
        "jdbc:mysql://localhost:3306/tedu?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true",
                "root",//数据库用户名
                "root");//数据库密码
        System.out.println("成功连接数据库");
        //3
        Statement statement=connection.createStatement();

        /*
        创建一张表
        create table userinfo(
    id int primary key auto_increment,
    username varchar(30),
    password varchar(30),
    nickname varchar(30),
    age int(3)
);
         */
        String sql="create table userinfo( " +
                "    id int primary key auto_increment, " +
                "    username varchar(30), " +
                "    password varchar(30), " +
                "    nickname varchar(30), " +
                "    age int(3) " +
                ")";
        statement.execute(sql);
        System.out.println("执行完毕");

        connection.close();
    }
}
